package com.gy.smart.home;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.base.MineBaseActivity;
import com.gy.smart.home.bean.DynamicTitleBean;
import com.gy.smart.util.FragmentAdapter;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.StringConstants;
import com.gy.smart.util.Urls;
import com.gy.smart.view.TitleView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2017/1/4.
 * Describe:昌平动态
 */
public class DynamicHomeActivity extends BaseActivity implements TitleView.TitleSelectListener {

    private TitleView youPinTitleView;
    private ViewPager viewPager;
    private FragmentAdapter fragmentAdapter;
    private List<Fragment> fragments;
    private List<String> titleList;
    private List<DynamicTitleBean> titles;
    private int page = 1;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_dynamic);
        setTitleMsg("昌平动态");
        youPinTitleView = (TitleView) findViewById(R.id.activity_dynamic_titleview);
        viewPager = (ViewPager) findViewById(R.id.activity_dynamic_viewpage);
    }

    @Override
    protected void initData() {
        fragments = new ArrayList<>();
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        titleList = new ArrayList<>();
        titles = new ArrayList<>();
    }

    @Override
    protected void setListener() {
        super.setListener();
        youPinTitleView.setMyListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                youPinTitleView.setSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        youPinTitleView.setmDatas(titleList);
        viewPager.setAdapter(fragmentAdapter);
        getTitles();
    }

    @Override
    public void titleSelect(int item) {
        viewPager.setCurrentItem(item);
    }

    /***/
    public void getTitles(){
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.url_news_titles);
        params.addParameter("page",page+"");
        params.addParameter("pagenum", StringConstants.PAGENUM+"");
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<DynamicTitleBean>>(){}.getType();
                    titles = gson.fromJson(result.data,type);
                    if(titles.size() > 0){
                        for (DynamicTitleBean titleBean : titles){
                            titleList.add(titleBean.cat_name);
                            fragments.add(new DynamicFragment(titleBean.cat_id));
                        }
                    }
                    youPinTitleView.setmDatas(titleList);
                    fragmentAdapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
